package utility;

import java.awt.Dimension;

import org.jgrapht.Graph;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;
import org.jgrapht.graph.Subgraph;

import observer.Broker;
import navigatore.AgenteNavigatore;
import jade.Boot;
import jade.tools.sniffer.Agent;
import jade.wrapper.AgentController;
import jade.wrapper.ContainerController;
import jade.wrapper.ControllerException;
import jade.wrapper.StaleProxyException;
import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.core.Runtime;


public class ManageJADEPlatform {

	private static AgentController agentController;
	private static ContainerController containerController;
	private static jade.Boot bootClass;
	private static AgenteNavigatore navigatore;
	
	//private static Broker broker;

	public static void startJADEPlatform() throws StaleProxyException
	{
		//start della piattaforma JADE
		bootClass = new Boot();
		bootClass.main(new String[]{"-gui"});

		startContainer();

	}

	public static void startContainer(){

		String host = "160.97.21.193";
		String port = "1099";

		Runtime runtime =Runtime.instance();

		Profile profile = new ProfileImpl();
		profile.setParameter(Profile.MAIN_HOST, host);
		profile.setParameter(Profile.MAIN_PORT, port);

		containerController = runtime.createAgentContainer(profile);
//		containerController = runtime.createMainContainer(profile);

		
		
	}
	
	//per ora non va!
//	public static void killAgenteNavigatore() throws ControllerException{
//		
//		try {
//			
////			agentController.suspend();
////			agentController.kill();
//			
//			containerController.getPlatformController().suspend();
//			containerController.getPlatformController().kill();
//			
//			
//		} catch (StaleProxyException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}

	//funzione che si occupa ti inserire un agente navigatore nella piattaforma facendolo partire
	public static AgenteNavigatore startNavigatore(Graph graph, Subgraph subGraph, int idNavigatore)
	{

		navigatore = new AgenteNavigatore((SimpleDirectedWeightedGraph<Triple<Dimension, String, String>, MyDefaultWeightedEdge>) graph,
				(Subgraph<Triple<Dimension,String,String>, MyDefaultWeightedEdge, Graph<Triple<Dimension,String,String>,MyDefaultWeightedEdge>>) subGraph);

		if(containerController != null)
		{
			try {

				//agentController = containerController.createNewAgent(name, "navigatore.AgenteNavigatore", null);
				agentController = containerController.acceptNewAgent("navigatore"+idNavigatore, navigatore);
				agentController.start();

			} catch (StaleProxyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		return navigatore;

	}


	public AgentController getAgentController() {
		return agentController;
	}

	public void setAgentController(AgentController agentController) {
		this.agentController = agentController;
	}

//	public static void killNavigatore(){
//
//				try {
//		
//					//agentController.kill();
//		
//					//containerController.kill();
//					
//				} catch (StaleProxyException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//	}


}
